package com.waijiaojun.tpo.web;

import com.waijiaojun.tpo.rest.RestClientException;
import com.waijiaojun.tpo.rest.RestResponse;
import com.waijiaojun.tpo.rest.RestServerException;
import com.waijiaojun.tpo.service.UserService;
import com.waijiaojun.tpo.utils.Constants;
import com.waijiaojun.tpo.utils.JsonUtil;
import com.waijiaojun.tpo.utils.UserUtil;
import com.waijiaojun.tpo.vo.IndexData;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created with IntelliJ IDEA.
 * User: xvitcoder
 * Date: 12/20/12
 * Time: 5:27 PM
 */
@Controller
@RequestMapping("/")
public class IndexController {

    private static Logger logger = LoggerFactory.getLogger(IndexController.class);

    @Autowired
    private UserService userService;

    @RequestMapping
    public String getIndexPage() {
        return "index-load.html";
    }

    @RequestMapping("index")
    public String index() {
        return "index";
    }

    @RequestMapping("index/data")
    @ResponseBody
    public RestResponse indexData() {
        RestResponse restResponse = null;
        try {
            //打印入参
            logger.info(Constants.prefix_in);
            String openid = UserUtil.getCurrentUserOpenId();
            IndexData data = userService.getIndexData(openid);
            restResponse = RestResponse.getSuccessResponse(data);
        } catch (RestClientException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getClientErrorResponse(e.getMessage());
        } catch (RestServerException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        }
        //打印出参
        logger.info(Constants.prefix_out + JsonUtil.toJson(restResponse));
        return restResponse;
    }

    @RequestMapping("share/{openid}")
    public String toSharePage(@PathVariable("openid") String openid, Model model) {
        //打印入参
        logger.info(Constants.prefix_in + "openid=" + openid);
        String qrUrl = userService.getQRUrl(openid);
        model.addAttribute("openid",openid);
        model.addAttribute("qrUrl",qrUrl);
        return "share";
    }

    @RequestMapping("share/data/{openid}")
    @ResponseBody
    public RestResponse shareData(@PathVariable("openid") String openid) {
        RestResponse restResponse = null;
        try {
            //打印入参
            logger.info(Constants.prefix_in + "openid=" + openid);
            if (StringUtils.isBlank(openid)) {
                throw new RestClientException("无效的参数：openid不能为空");
            }
            IndexData data = userService.getIndexData(openid);
            restResponse = RestResponse.getSuccessResponse(data);
        } catch (RestClientException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getClientErrorResponse(e.getMessage());
        } catch (RestServerException e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            restResponse = RestResponse.getServerErrorResponse(e.getMessage());
        }
        //打印出参
        logger.info(Constants.prefix_out + JsonUtil.toJson(restResponse));
        return restResponse;
    }


}
